// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.SystemColor;
import javax.swing.JTextField;
import java.awt.Insets;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.miginfocom.swing.MigLayout;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.AbstractButton;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.UIManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.gatech.oad.cyberdoc.domain.Appointment;
import edu.gatech.oad.cyberdoc.domain.AppointmentSpec;
import edu.gatech.oad.cyberdoc.presenter.Presenter;
import javax.swing.BoxLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;


public class AppointmentViewPanel extends ApplicationPanel {
	

	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	static {
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.action."
				+ "ActionHandler");
	}

	JSONArray appts;
	private JList apptsList;
	private JTextField patientField;
	private JTextField doctorField;
	private JTextField reasonField;
	private JLabel lblStatus;

	/**
	 * Create the panel.
	 */
	public AppointmentViewPanel() {
		setBackground(SystemColor.inactiveCaptionBorder);
		setLayout(null);
		
		JPanel topHalf = new JPanel();
		topHalf.setBackground(SystemColor.inactiveCaptionBorder);
		topHalf.setBounds(0, 0, 584, 195);
		add(topHalf);
		topHalf.setLayout(null);
		
		JPanel toolbar = new JPanel();
		toolbar.setBackground(SystemColor.inactiveCaptionBorder);
		FlowLayout fl_toolbar = (FlowLayout) toolbar.getLayout();
		fl_toolbar.setAlignment(FlowLayout.LEFT);
		toolbar.setBounds(10, 11, 564, 33);
		topHalf.add(toolbar);
		
		JLabel lblPleaseSelectA = new JLabel("Please select a appointment to view");
		toolbar.add(lblPleaseSelectA);
		
		JScrollPane tablePanel = new JScrollPane();
		tablePanel.setBounds(10, 48, 564, 136);
		topHalf.add(tablePanel);
		
		apptsList = new JList();
		apptsList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				int index = apptsList.getSelectedIndex();

				if (index != -1) {
					String id = apptsList.getModel().getElementAt(index)
							.toString();
					Presenter.getInstance().readAppts(id);
				}
			}
		});
		tablePanel.setViewportView(apptsList);
		
		JLabel lblPleaseSelctA = new JLabel("Appointment Info:");
		lblPleaseSelctA.setBounds(10, 193, 366, 14);
		add(lblPleaseSelctA);
		
		JPanel bottomHalf = new JPanel();
		bottomHalf.setBounds(10, 215, 566, 142);
		add(bottomHalf);
		bottomHalf.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		JLabel lblPatient = new JLabel("Patient: ");
		bottomHalf.add(lblPatient, "2, 4, right, default");
		
		patientField = new JTextField();
		patientField.setEditable(false);
		bottomHalf.add(patientField, "4, 4, fill, default");
		patientField.setColumns(10);
		
		JLabel lblDoctor = new JLabel("Doctor: ");
		bottomHalf.add(lblDoctor, "2, 6, right, default");
		
		doctorField = new JTextField();
		doctorField.setEditable(false);
		bottomHalf.add(doctorField, "4, 6, fill, default");
		doctorField.setColumns(10);
		
		JLabel lblReason = new JLabel("Reason: ");
		bottomHalf.add(lblReason, "2, 8, right, default");
		
		reasonField = new JTextField();
		bottomHalf.add(reasonField, "4, 8, fill, default");
		reasonField.setColumns(10);
			
	}

	@Override
	public void show(JSONObject response)
	{
		try 
		{
			String action = response.getString("action");
			JSONArray times = response.getJSONArray("appts");
			Object[] data = new Object[times.length()];

			
			for (int i = 0; i < times.length(); i++) 
			{
				// TODO Put this into a more readable format
				JSONObject appt = times.getJSONObject(i);
				JSONObject spec =  appt.getJSONObject("spec");
				long dateIndex = spec.getLong("date");
				data[i] = new Date(dateIndex).toString();
			}
			apptsList.setListData(data);
			patientField.setText("");
			doctorField.setText("");
			reasonField.setText("");
			lblStatus.setText("");
			if (action.equals("read_user")) 
			{
				JSONObject user = response.getJSONObject("user");
				LOG.info("Showing user " + user.getString("username"));

				patientField.setText(user.getString("username"));
				doctorField.setText(user.getString("type"));
			} 
			else if(action.equals("update_user")) 
			{
				LOG.info("User updated");
				
				lblStatus.setText(patientField.getText() + "'s account updated.");
			} 
			else 
			{
				LOG.warning("Panel shown for unexpected action");
			}
		}	
		catch(JSONException e) 
		{
			LOG.log(Level.SEVERE, "", e);
		}
	}
}	
